package net.common.util;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

/**
 * 资源查找类(从类路径、classpath、查找指定文件)
 * 
 * @author 孙树林
 * 
 */
public class ResourceQuery {

	/** 文件 */
	public static final String FILE_URL_PREFIX = "file:";
	/** jar文件 */
	public static final String URL_PROTOCOL_JAR = "jar:";
	/** claspath */
	public static final String CLASSPATH_URL_PREFIX = "classpath:";

	/**
	 * 查找资源文件根据partten判断从哪查询资源文件.如果partten是NULL值查询所有
	 * 
	 * @param partten
	 * @return
	 * @throws Exception
	 */
	public List<URL> findResource(String partten) throws Exception {
		List<URL> urls = new ArrayList<URL>();
		if (StringUtils.startswith(partten, CLASSPATH_URL_PREFIX)) {
			partten = partten.substring(CLASSPATH_URL_PREFIX.length());
			findResourceByClassPath(urls, partten);
		} else if (StringUtils.startswith(partten, FILE_URL_PREFIX)) {
			partten = partten.substring(FILE_URL_PREFIX.length());
			int end = partten.lastIndexOf("/");
			if (end != -1) {
				File file = new File(partten.substring(0, end));
				partten = partten.substring(end);
				findResourcesByFile(urls, file, partten);
			}
		} else if (StringUtils.startswith(partten, URL_PROTOCOL_JAR)) {
			partten = partten.substring(URL_PROTOCOL_JAR.length());
			String path = ResourceResolver.class.getClassLoader().getResource("").getPath().replaceAll("classes", "lib");
			findResourceByJar(urls, new File(path), partten);
		} else {
			findResourceByClassPath(urls, partten);
			String path = ResourceResolver.class.getClassLoader().getResource("").getPath().replaceAll("classes", "lib");
			findResourceByJar(urls, new File(path), partten);
		}
		return urls;
	}

	/**
	 * 从classpath路径查找资源文件
	 * 
	 * @param urls
	 * @param partten
	 * @throws Exception
	 */
	public void findResourceByClassPath(List<URL> urls, String partten) throws Exception {
		URL url = ResourceResolver.class.getClassLoader().getResource("");
		File file = new File(url.getPath()); // class类路径
		findResourcesByFile(urls, file, partten);
	}

	/**
	 * 从指定文件路径下搜索资源文件
	 * 
	 * @param urls
	 * @param file
	 * @param partten
	 * @throws Exception
	 */
	public void findResourcesByFile(List<URL> urls, File file, String partten) throws Exception {
		if (file.isFile()) {
			if (StringUtils.match(file.getName(), "^" + partten.replace("*", ".*") + "$")) {
				urls.add(new URL(FILE_URL_PREFIX + file.getAbsolutePath()));
			}
		}
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File dir = files[i];
			if (dir.isDirectory()) {
				findResourcesByFile(urls, dir, partten);
			} else {
				if (StringUtils.match(dir.getName(), "^" + partten.replace("*", ".*") + "$")) {
					urls.add(new URL(FILE_URL_PREFIX + dir.getAbsolutePath()));
				}
			}
		}
	}

	/**
	 * 从jar包读取
	 * 
	 * @param filePath
	 * @param partten
	 * @throws Exception
	 */
	public void findResourceByJar(List<URL> urls, File file, String partten) throws Exception {
		if (file.isFile()) {
			List<String> files = JarUtils.searchByJar(file, "^" + partten.replace("*", ".*") + "$");
			for (String f : files) {
				urls.add(new URL("jar:file:/" + f));
			}
			return;
		}
		File[] files = file.listFiles();
		for (int i = 0; i < files.length; i++) {
			File dir = files[i];
			if (!dir.isDirectory() && dir.getPath().endsWith(".jar")) {
				List<String> fl = JarUtils.searchByJar(dir, "^" + partten.replace("*", ".*") + "$");
				for (String f : fl) {
					String u = "jar:file:\\" + dir + "!/" + f;
					urls.add(new URL(u));
				}
			}
		}
	}

}
